package com.yuanian.ecsplus.converter;

import com.yuanian.businessobject.model.enums.bodefine.BOColumnDataTypeEnum;
import com.yuanian.businessobject2.model.vo.defervalue.DeferredReferenceValueMap;
import com.yuanian.console.entity.ConsoleUserDO;
import com.yuanian.ecsplus.entity.OneRefEntity;
import com.yuanian.ecsplus.exception.EcsPlusException;
import com.yuanian.ecsplus.context.ReflectContext;
import com.yuanian.ecsplus.meta.entity.MetaEntity;
import com.yuanian.ecsplus.reflect.PersistenceMetaClass;
import com.yuanian.ecsplus.reflect.SimpleMetaClass;
import com.yuanian.ecsplus.entity.DictionaryEntity;
import com.yuanian.ecsplus.reflect.SimplePersistenceMetaClass;
import com.yuanian.infrastructure.util.service.JsonUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

/**
 * 其他类型的转换器
 */
public class CommonTypeConverter implements TypeConverter {

    @Override
    public Object obj2Map(ReflectContext reflectContext) {
        if (reflectContext.getObject() == null || reflectContext.getField() == null) {
            throw new EcsPlusException("转换对象为null");
        }
        try {
            Object obj = reflectContext.getRawResult();
            if(obj == null) {
                return null;
            }
            if (DictionaryEntity.class.isAssignableFrom(obj.getClass())) {
                return ((DictionaryEntity) obj).getCode();
            } else if (OneRefEntity.class.isAssignableFrom(obj.getClass())) {
                return ((OneRefEntity) obj).getObjectId();
            } //else if (List.class.isAssignableFrom(reflectContext.getField().getType())) {
              else if (reflectContext.getBOAreaFieldSetupVO() != null && reflectContext.getBOAreaFieldSetupVO().getDataType() == BOColumnDataTypeEnum.MREF) {//多值
                String str = "";
                List list = (List)obj;
                for (int i = 0; i < list.size(); i++) {
                    Object obj1 = list.get(i);
                    PersistenceMetaClass p = new SimplePersistenceMetaClass(obj1.getClass());
                    str = str + p.getIdField().getField().get(obj1) + ",";
                }
                return str.length() == 0 ? str : str.substring(0, str.length() - 1);
            }
            return obj;
        } catch (IllegalAccessException e) {
            throw new EcsPlusException(e.getCause());
        }
    }

    @Override
    public Object map2Obj(ReflectContext reflectContext) {
        if (reflectContext.getMap() == null || reflectContext.getField() == null) {
            return null;
        }
        if (reflectContext.getField().getType() == MetaEntity.class) {
            return null;
        }
        //获取MAP对应字段的数据
        Object mapValue = reflectContext.getMapData();

        if (mapValue instanceof DeferredReferenceValueMap) {
            Class clz = reflectContext.getField().getType();
            SimpleMetaClass simpleMetaClass = new SimpleMetaClass(clz);
            Object obj = simpleMetaClass.newInstance();
            simpleMetaClass.copyProperty(obj, (DeferredReferenceValueMap)mapValue);
            return obj;
        } //else if (mapValue instanceof Collection) {//多值
            else if (reflectContext.getBOAreaFieldSetupVO() != null && reflectContext.getBOAreaFieldSetupVO().getDataType() == BOColumnDataTypeEnum.MREF) {//多值
            Class clz = reflectContext.getField().getType();
            //SimpleMetaClass simpleMetaClass = new SimpleMetaClass(clz);

            //Object obj = simpleMetaClass.newInstance();
            ParameterizedType type = (ParameterizedType)reflectContext.getField().getGenericType();
            Type type1 = type.getActualTypeArguments()[0];

            List nums=new ArrayList();

            Collection map = (Collection)mapValue;

            for (Object entry : map) {
                SimpleMetaClass simpleMetaClass1 = new SimpleMetaClass((Class)type1);
                Object o = simpleMetaClass1.newInstance();
                DeferredReferenceValueMap e = (DeferredReferenceValueMap)entry;
                simpleMetaClass1.copyProperty(o, e);
                nums.add(o);
            }
            return nums;
        }
        //类型不兼容
        if (mapValue != null && reflectContext.getField().getType() != mapValue.getClass()) {
            throw new EcsPlusException(reflectContext.getField().getType() + ":" + mapValue.getClass() + "类型不兼容");
        }
        return mapValue;
    }
}
